package com.example.springsecuritydemo.support;

import lombok.Data;

/**
 * 统一返回结果类
 *
 * @author chenggang
 * @date 2019/04/08
 */
@Data
public class RestResult<T> {

    private int code;

    private String message;

    private T data;

    public RestResult() {

    }

    public RestResult(int code, String message, T data) {
        super();
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> RestResult<T> success(T data) {
        RestResult<T> result = new RestResult<T>();
        result.setCode(EnumResultCode.SUCCESS.getCode());
        result.setMessage(EnumResultCode.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> RestResult<T> success() {
        RestResult<T> result = new RestResult<T>();
        result.setCode(EnumResultCode.SUCCESS.getCode());
        result.setMessage(EnumResultCode.SUCCESS.getMsg());
        return result;
    }

    public static <T> RestResult<T> fail(T data) {
        RestResult<T> result = new RestResult<T>();
        result.setCode(EnumResultCode.FAIL.getCode());
        result.setMessage(EnumResultCode.FAIL.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> RestResult<T> fail(String msg) {
        RestResult<T> result = new RestResult<T>();
        result.setCode(EnumResultCode.FAIL.getCode());
        result.setMessage(msg);
        return result;
    }


    public enum EnumResultCode {
        SUCCESS(1000, "成功"),
        FAIL(1001, "失败");

        private int code;
        private String msg;

        private EnumResultCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

    }
}
